package br.ime.usp.mestrado.cosen.jboss.mc.util;

import java.net.URL;

import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;

public class MCBootstrap extends BasicBootstrap {

	private BasicXMLDeployer deployer;

	public void bootstrap() throws Throwable {
		super.bootstrap();

		this.deployer = new BasicXMLDeployer(super.getKernel());

		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				MCBootstrap.this.deployer.shutdown();
			}
		});
	}

	public void deploy(URL url) {
		if (this.deployer.getDeploymentNames().contains(url.toString())) {
			log.warn("Service is already deployed.");
		} else {
			try {
				this.deployer.deploy(url);
			} catch (Throwable t) {
				log.error("Error during deployment: " + url, t);
			}
		}
	}

	public void undeploy(URL url) {
		if (!this.deployer.getDeploymentNames().contains(url.toString())) {
			log.warn("Service is already undeployed.");
		} else {
			try {
				deployer.undeploy(url);
			} catch (Throwable t) {
				log.error("Error during undeployment: " + url, t);
			}
		}
	}

}
